package com.gxg.newdemo1.mynewdamo1.ca;

import java.util.HashMap;
import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.gxg.newdemo1.mynewdamo1.R;
import com.socks.library.KLog;

import cn.org.bjca.signet.CertType;
import cn.org.bjca.signet.helper.bean.OCRInfoBean;
import cn.org.bjca.signet.sdk.MessageControler;
import cn.org.bjca.signet.sdk.OfflineSignResultEntity;
import cn.org.bjca.signet.sdk.ResultEntity;
import cn.org.bjca.signet.sdk.SignetCallBack;
import cn.org.bjca.signet.sdk.SignetSDKInstance;
import cn.org.bjca.signet.sdk.UserEntity;

public class TestTwoActivity extends Activity implements SignetCallBack {

	private SignetSDKInstance sdkInstance; // SDK实例
	private String appId = "67f6c99b74484c2b8bcb35473850cdd7";
	private String msspID = "e6333fcd892948ea61954d3107eb8e8d9b21f4d419f0cdf3805b4226a65bd974";
	private String enMsspId = "ENA_a68e7f48-dc40-4d04-a602-55426873d6e3";

	private Button btnEnableFinger, btnDisableFinger;
	private Button btnEnterSeal, btnEnterSealImg;
	private Button btnGetDevId, btnGetSignImg, btnGetUserCert, btnGetUserList;
	private Button btnAnalyzeCert, btnClearCert;
	private Button btnGetOcr, btnMutipalFindBack;

	private Button btnReqOfflineCert, btnOfflineSign;
	private Button btnSignDocuInit, btnGetDocuInfo;

	private Button btnClearOfflineCert;

	private Button btnNextPage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test_two);

		sdkInstance = SignetSDKInstance.getInstance(appId);
		findView();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (sdkInstance != null) {
			MessageControler.messageHandle(TestTwoActivity.this, data);
		}
	}

	private void findView() {

		// 开启指纹
		btnEnableFinger = (Button) findViewById(R.id._test_enable_finger);
		btnEnableFinger.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sdkInstance.enableFingerSign(TestTwoActivity.this, msspID);

			}
		});

		// 关闭指纹
		btnDisableFinger = (Button) findViewById(R.id._test_disable_finger);
		btnDisableFinger.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sdkInstance.disableFingerSign(TestTwoActivity.this, msspID);

			}
		});

		// 获取企业签章图片
		btnEnterSeal = (Button) findViewById(R.id._test_enter_seal);
		btnEnterSeal.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sdkInstance.enterpriseSeal(TestTwoActivity.this, enMsspId);

			}
		});

		// 获取企业签章图片代码
		btnEnterSealImg = (Button) findViewById(R.id._test_enter_seal_img);
		btnEnterSealImg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sdkInstance.getEnterpriseSeal(TestTwoActivity.this, enMsspId);
			}
		});

		// 获取设备标识
		btnGetDevId = (Button) findViewById(R.id._test_get_dev_id);
		btnGetDevId.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String devId = sdkInstance.getDeviceId(TestTwoActivity.this);
				Toast.makeText(TestTwoActivity.this, devId, Toast.LENGTH_SHORT).show();
			}
		});

		// 获取个人用户签章图片
		btnGetSignImg = (Button) findViewById(R.id._test_get_sign_img);
		btnGetSignImg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String signImg = sdkInstance.getSignImage(TestTwoActivity.this, enMsspId);

				Toast.makeText(TestTwoActivity.this, signImg, Toast.LENGTH_SHORT).show();
			}
		});

		// 获取用户证书
		btnGetUserCert = (Button) findViewById(R.id._test_get_user_cert);
		btnGetUserCert.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				HashMap<CertType, String> certMap = sdkInstance.getUserCert(TestTwoActivity.this, msspID,
						CertType.RSA_LOGIN_CERT);
				Toast.makeText(TestTwoActivity.this, certMap.get(CertType.RSA_LOGIN_CERT), Toast.LENGTH_SHORT).show();
			}
		});

		// 获取当前设备用户列表
		btnGetUserList = (Button) findViewById(R.id._test_get_user_list);
		btnGetUserList.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				HashMap<String, String> userList = sdkInstance.getUserList(TestTwoActivity.this);
				Set<String> strings = userList.keySet();
				for (String string : strings) {
					KLog.e("sss  "+string+"   "+userList.get(string));
				}
				Toast.makeText(TestTwoActivity.this, userList.toString(), Toast.LENGTH_SHORT).show();
			}
		});

		// 解析用户证书
		btnAnalyzeCert = (Button) findViewById(R.id._test_analyze_cert);
		btnAnalyzeCert.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String cert = "";

				HashMap<String, String> certInfoMap = sdkInstance.analyzeCert(cert);
				Toast.makeText(TestTwoActivity.this, certInfoMap.toString(), Toast.LENGTH_SHORT).show();

			}
		});

		// 删除用户证书
		btnClearCert = (Button) findViewById(R.id._test_clear_cert);
		btnClearCert.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sdkInstance.clearCert(TestTwoActivity.this, enMsspId);

			}
		});

		// 获取OCR信息
		btnGetOcr = (Button) findViewById(R.id._test_get_ocr);
		btnGetOcr.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sdkInstance.selfRegGetOCR(TestTwoActivity.this);

			}
		});

		// 多途径找回
		btnMutipalFindBack = (Button) findViewById(R.id._test_find_back_mutipal);
		btnMutipalFindBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				sdkInstance.findbackUserWithCredential(TestTwoActivity.this, "110116198107268807", "SF", "王刚");

			}
		});

		// 申请离线证书，需后台配置离线策略方可调用
		btnReqOfflineCert = (Button) findViewById(R.id._test_req_offline_cert);
		btnReqOfflineCert.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sdkInstance.reqOfflineCert(TestTwoActivity.this, msspID);

			}
		});

		// 离线签名接口
		btnOfflineSign = (Button) findViewById(R.id._test_offline_sign);
		btnOfflineSign.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String dataType = "CLEAR_DATA";
				String algoPolicy = "sm3withsm2";
				String text = "哈哈dsf哈哈哈";
				sdkInstance.offlineSign(TestTwoActivity.this, msspID, algoPolicy, dataType, text);

			}
		});

		// 带签名图片签文档
		btnSignDocuInit = (Button) findViewById(R.id._test_sign_doc_init);
		btnSignDocuInit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String docuID = "ENDOCG_79c4c43e-4640-48f7-9dea-2bd618420a51";
				String image = "R0lGODlhyABnAPcAAN8AAN4AAN4AAN4AAN4AAN4AAN4AAN4AAN4AAN4AAN4AAN4AAN4AAN4AAMsAAJEAAAsAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAICAg0NDSAgID8/P0BAQEFBQUJCQkNDQ0REREVFRUZGRkdHR0hISElJSUpKSktLS0xMTE1NTU5OTk9PT1BQUFFRUVJSUlNTU1RUVFVVVVZWVldXV1hYWFlZWVpaWltbW1xcXF1dXV5eXl9fX2BgYGFhYWJiYmNjY2RkZGVlZWZmZmdnZ2hoaGlpaWpqamtra2xsbG1tbW5ubm9vb3BwcHFxcXJycnNzc3R0dHV1dXZ2dnd3d3h4eHl5eXp6ent7e3x8fH19fX5+fn9/f4CAgIGBgYKCgoODg4SEhIWFhYaGhoeHh4iIiImJiYqKiouLi4yMjI2NjY6Ojo+Pj5CQkJGRkZKSkpOTk5SUlJWVlZaWlpeXl5iYmJmZmZqampubm5ycnJ2dnZ6enp+fn6CgoKGhoaKioqOjo6SkpKWlpaampqenp6ioqKmpqaqqqqurq6ysrK2tra6urq+vr7CwsLGxsbKysrOzs7S0tLW1tba2tre3t7i4uLm5ubq6uru7u7y8vL29vb6+vr+/v8DAwMHBw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ds8MEIJ6zwwgw37PDDEEcs8cQUyxsQADs=";
				sdkInstance.signDocuInit(TestTwoActivity.this, msspID, docuID, image);

			}
		});

		// 获取签章位置信息
		btnGetDocuInfo = (Button) findViewById(R.id._test_get_doc_info);
		btnGetDocuInfo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String docuId = "ENDOCG_79c4c43e-4640-48f7-9dea-2bd618420a51";
				sdkInstance.getDocuInfo(TestTwoActivity.this, msspID, docuId);

			}
		});

		btnClearOfflineCert = (Button) findViewById(R.id._test_clear_offline_cert);
		btnClearOfflineCert.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sdkInstance.clearOfflineCert(TestTwoActivity.this, msspID);

			}
		});
	}

	@Override
	public void loginCallBack(ResultEntity result) {
		// TODO Auto-generated method stub

	}

	@Override
	public void qrLoginCallBack(ResultEntity result) {
		// TODO Auto-generated method stub

	}

	@Override
	public void registerCallBack(ResultEntity result) {
		// TODO Auto-generated method stub

	}

	@Override
	public void qrRegisterCallBack(ResultEntity result) {
		// TODO Auto-generated method stub

	}

	@Override
	public void signDataCallBack(ResultEntity result) {
		// TODO Auto-generated method stub

	}

	@Override
	public void qrSignDataCallBack(ResultEntity result) {
		// TODO Auto-generated method stub

	}

	@Override
	public void findBackUserCallBack(ResultEntity result) {
		if (result.getStatus().equalsIgnoreCase("0000")) {
			Toast.makeText(TestTwoActivity.this, "找回用户成功", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(TestTwoActivity.this, "找回用户失败", Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void signDocuCallBack(ResultEntity result) {
		if (result.getStatus().equalsIgnoreCase("0000")) {
			Toast.makeText(TestTwoActivity.this, "文档签署成功", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(TestTwoActivity.this, "文档签署失败", Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void setHandWritingCallBack(ResultEntity result) {
		// TODO Auto-generated method stub

	}

	@Override
	public void selfRegisterCallBack(ResultEntity result) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setFingerCallBack(ResultEntity result) {
		if (result.getStatus().equalsIgnoreCase("0000")) {
			Toast.makeText(TestTwoActivity.this, "指纹设置成功", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(TestTwoActivity.this, "指纹设置失败", Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void selfRegInfoCallBack(UserEntity result) {
		// TODO Auto-generated method stub

	}

	@Override
	public void selfRegGetOcrCallBack(OCRInfoBean result) {
		if (result.getStatus().equalsIgnoreCase("0000")) {
			Toast.makeText(TestTwoActivity.this, "获取OCR信息成功", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(TestTwoActivity.this, "获取OCR信息失败", Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void enterPriseSealCallBack(ResultEntity result) {
		if (result.getStatus().equalsIgnoreCase("0000")) {
			Toast.makeText(TestTwoActivity.this, "获取企业签章图片成功", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(TestTwoActivity.this, "获取企业签章图片失败", Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void reqOfflineCertCallBack(ResultEntity result) {

		if (result.getStatus().equalsIgnoreCase("0000")) {
			Toast.makeText(TestTwoActivity.this, "申请离线证书成功", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(TestTwoActivity.this, "申请离线证书失败", Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void offlineSignCallBack(OfflineSignResultEntity result) {
		if (result.getStatus().equalsIgnoreCase("0000")) {
			Toast.makeText(TestTwoActivity.this, "离线签名成功", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(TestTwoActivity.this, "离线签名失败", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void enterPriseSealSingleCallBack(ResultEntity result) {
		if (result.getStatus().equalsIgnoreCase("0000")) {
			Toast.makeText(TestTwoActivity.this, "获取企业签章图片成功", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(TestTwoActivity.this, "获取企业签章图片失败", Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void enterPriseSealImageSingleCallBack(ResultEntity result) {
		if (result.getStatus().equalsIgnoreCase("0000")) {
			Toast.makeText(TestTwoActivity.this, "获取企业签章图片成功", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(TestTwoActivity.this, "获取企业签章图片失败", Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void getDocuInfoCallBack(ResultEntity result) {

		if (result.getStatus().equalsIgnoreCase("0000")) {
			Toast.makeText(TestTwoActivity.this, "获取文档签章信息成功", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(TestTwoActivity.this, "获取文档签章信息失败", Toast.LENGTH_SHORT).show();
		}

	}
}
